package com.example.wechattakeaway.controller;

import com.example.wechattakeaway.form.orderform.InsertOrderDetailForm;
import com.example.wechattakeaway.form.orderform.OrderCommitForm;
import com.example.wechattakeaway.service.OrderDetailService;
import com.example.wechattakeaway.service.OrderService;
import com.example.wechattakeaway.vo.ResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;

@Slf4j
@Api(value = "商家订单管理",tags = {"商家订单操作接口"})
@RestController
@RequestMapping("/MerOrderController")
public class MerOrderController {

    @Autowired
    OrderService orderService;

    @Autowired
    OrderDetailService orderDetailService;


    @ApiOperation(value = "用户提交订单,改变订单状态",notes = "微信端")
    @PostMapping("/UserCommit")
    public ResultVO UserCommit(OrderCommitForm orderCommitForm) throws ParseException {
        log.info(orderCommitForm.getUserId().toString());
        return orderService.UserCommit(orderCommitForm);
    }

    @ApiOperation(value = "插入订单详情表",notes = "该接口与提交订单一起使用")
    @PostMapping("/InsertOrderDetail")
    public ResultVO InsertOrderDetail(InsertOrderDetailForm insertOrderDetailForm){
        return orderDetailService.InsertOrderDetail(insertOrderDetailForm);
    }

    @ApiOperation(value = "用户支付，改变订单状态",notes = "微信端")
    @PostMapping("/UserPayment")
    public ResultVO UserPayment(Integer orderId){
        return orderService.UserPayment(orderId);
    }

    @ApiOperation(value = "商家确认，改变订单状态",notes = "网页端")
    @PostMapping("/MerchantConfirm")
    public ResultVO MerchantConfirm(Integer orderId){
        return orderService.MerchantConfirm(orderId);
    }

    @ApiOperation(value = "用户确认外卖送达，改变订单状态",notes = "微信端")
    @PostMapping("/UserConfirm")
    public ResultVO UserConfirm(Integer orderId){
        return orderService.UserConfirm(orderId);
    }

    //商家确认，用户确认之后，不可取消订单，双方确认之前都可以取消
    @ApiOperation(value = "商家或用户任意一方取消订单，改变订单状态",notes = "微信and网页")
    @PostMapping("/OrderQuit")
    public ResultVO OrderQuit(Integer orderId){
        return orderService.OrderQuit(orderId);
    }

    @ApiOperation(value = "根据id查询订单",notes = "微信and网页")
    @PostMapping("/SelectOrderById")
    public ResultVO SelectOrderById(Integer orderId){
        return orderService.SelectOrderById(orderId);
    }

    @ApiOperation(value = "根据用户id时间倒叙查询订单",notes = "微信")
    @PostMapping("/SelectOrderByUser")
    public ResultVO SelectOrderByUser(Integer pageNum,Integer userId){
        return orderService.SelectOrderByUser(pageNum,userId);
    }

    @ApiOperation(value = "根据商家id时间倒叙查询订单",notes = "网页")
    @GetMapping("/SelectOrderByMerchant")
    public ResultVO SelectOrderByMerchant(@RequestParam("page") Integer page,@RequestParam("merchantId") Integer merchantId){
        log.info("当前默认页数是"+page.toString());
        return orderService.SelectOrderByMerchant(page,merchantId);
    }

    @ApiOperation(value = "根据id删除订单",notes = "只有系统管理员有权限")
    @PostMapping("/DeleteOrderById")
    public ResultVO DeleteOrderById(Integer orderId){
        return orderService.DeleteOrderById(orderId);
    }

}
